開始使用

本節提供關於如何開始使用 Spring AI 的起點。

您應根據您的需求,依照以下各節的步驟進行操作。

Spring AI 支援 Spring Boot 3.2.x 和 3.3.x

Spring Initializr

前往 start.spring.io 並選取您想在新應用程式中使用的 AI 模型和向量儲存庫。

新增里程碑和快照儲存庫

如果您偏好手動新增依賴程式碼片段,請依照以下各節的指示操作。

若要使用里程碑和快照版本,您需要在您的組建檔案中新增對 Spring 里程碑和/或快照儲存庫的參考。

將以下儲存庫定義新增至您的 Maven 或 Gradle 組建檔案

  • Maven

  • Gradle

<repositories>
  <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
      <enabled>false</enabled>
    </snapshots>
  </repository>
  <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <releases>
      <enabled>false</enabled>
    </releases>
  </repository>
</repositories>
repositories {
  mavenCentral()
  maven { url 'https://repo.spring.io/milestone' }
  maven { url 'https://repo.spring.io/snapshot' }
}

依賴管理

Spring AI 材料清單 (BOM) 宣告了 Spring AI 特定版本所使用之所有依賴項的建議版本。從您應用程式的組建腳本中使用 BOM 可避免您自行指定和維護依賴項版本的需求。取而代之的是,您使用的 BOM 版本決定了所使用的依賴項版本。它也確保您預設使用受支援和經過測試的依賴項版本,除非您選擇覆寫它們。

將 BOM 新增至您的專案

  • Maven

  • Gradle

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
dependencies {
  implementation platform("org.springframework.ai:spring-ai-bom:1.0.0-SNAPSHOT")
  // Replace the following with the starter dependencies of specific modules you wish to use
  implementation 'org.springframework.ai:spring-ai-openai'
}

Gradle 使用者也可以透過利用 Gradle (5.0+) 原生支援,使用 Maven BOM 宣告依賴約束來使用 Spring AI BOM。這是透過將 'platform' 依賴處理方法新增到您 Gradle 組建腳本的 dependencies 區段來實作的。如下面的程式碼片段所示,接著可以針對您希望使用的一個或多個 spring-ai 模組進行無版本宣告的 Starter 依賴項,例如 spring-ai-openai。

為特定組件新增依賴項

文件中的以下各節說明您需要將哪些依賴項新增到您的專案組建系統中。

範例專案

您可以從 GitHub 克隆這些專案以開始使用。

航班預訂助理

AI 驅動的系統,可以存取條款和條件 (檢索增強生成,RAG)、存取工具 (Java 方法) 以執行動作 (函數呼叫),並使用 LLM 與使用者互動